﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="Transform2D.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2010, 2011
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Collision.
//
//   starLiGHT.Collision is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Collision is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Collision. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Collision are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 3345                  $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2011-01-13 16:01:08 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.Collision
{
    #region Using Statement
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    #endregion

    public class Transform2D : IEquatable<Transform2D>
    {
        private Matrix transformationMatrix;
        private bool dirty;
        private Matrix originMatrix;
        private bool originMatrixDirty;
        private Matrix scaleMatrix;
        private bool scaleMatrixDirty;
        private Matrix translationMatrix;
        private bool translationMatrixDirty;
        private Matrix rotationMatrix;
        private bool rotationMatrixDirty;

        private Vector2? origin;
        private Vector2? position;
        private Vector2? scale;
        private float orientation;

        public Transform2D(Vector2 position)
        {
            this.dirty = true;
            this.position = position; 
            this.translationMatrixDirty = true;
            this.origin = null; 
            this.originMatrixDirty = true;
            this.scale = null; 
            this.scaleMatrixDirty = true;
            this.orientation = 0f; 
            this.rotationMatrixDirty = true;
            
            this.transformationMatrix = Matrix.Identity;
            this.originMatrix = Matrix.Identity;
            this.scaleMatrix = Matrix.Identity;
            this.translationMatrix = Matrix.Identity;
            this.rotationMatrix = Matrix.Identity;
        }

        public Transform2D(Vector2 position, float orientation)
            : this(position)
        {
            this.orientation = orientation;
        }

        public Transform2D(Vector2 position, float orientation, float scale)
            : this(position, orientation)
        {
            this.scale = new Vector2(scale, scale);
        }

        public Transform2D(Vector2 position, float orientation, Vector2 scale)
            : this(position, orientation)
        {
            this.scale = scale;
        }

        public Transform2D(Vector2 position, Vector2 origin)
            : this(position)
        {
            this.origin = origin;
        }

        public Transform2D(Vector2 position, Vector2 origin, float orientation)
            : this(position, orientation)
        {
            this.origin = origin;
        }

        public Transform2D(Vector2 position, Vector2 origin, float orientation, float scale)
            : this(position, orientation, scale)
        {
            this.origin = origin;
        }

        public Transform2D(Vector2 position, Vector2 origin, float orientation, Vector2 scale)
            : this(position, orientation, scale)
        {
            this.origin = origin;
        }

        public event EventHandler<EventArgs> Changed;

        /// <summary>
        /// Gets a value indicating whether the orientation is not equal zero or the scale is not one.
        /// </summary>
        public bool IsTransformed
        {
            get
            {
                return this.orientation != 0f || (this.scale.HasValue && this.scale.Value != Vector2.One);
            }
        }

        public Vector2? Origin
        {
            get 
            { 
                return this.origin; 
            }

            set 
            {
                if (this.origin != value)
                {
                    this.originMatrixDirty |= true;
                    this.origin = value;
                    
                    if (this.Changed != null)
                    {
                        this.Changed(this, null);
                    }
                }
            }
        }

        public Vector2? Position
        {
            get 
            { 
                return this.position; 
            }

            set 
            {
                if (this.position != value)
                {
                    this.translationMatrixDirty |= true;
                    this.position = value;

                    if (this.Changed != null)
                    {
                        this.Changed(this, null);
                    }
                }
            }
        }

        public Vector2? Scale
        {
            get 
            { 
                return this.scale; 
            }

            set
            {
                if (this.scale != value)
                {
                    this.scaleMatrixDirty |= true;
                    this.scale = value;

                    if (this.Changed != null)
                    {
                        this.Changed(this, null);
                    }
                }
            }
        }

        public float Orientation
        {
            get 
            { 
                return this.orientation; 
            }

            set
            {
                if (Math.Abs(this.orientation - value) > float.Epsilon)
                {
                    this.rotationMatrixDirty |= true;
                    this.orientation = value;

                    if (this.Changed != null)
                    {
                        this.Changed(this, null);
                    }
                }
            }
        }

        public Matrix TransformationMatrix
        {
            get 
            {
                this.UpdateMatrix();
                return this.transformationMatrix; 
            }
        }

        public void Clear()
        {
            this.dirty = true;
            this.origin = null;
            this.originMatrixDirty = true;
            this.position = null;
            this.translationMatrixDirty = true;
            this.scale = null;
            this.scaleMatrixDirty = true;
            this.orientation = 0f;
            this.rotationMatrixDirty = true;

            if (this.Changed != null)
            {
                this.Changed(this, null);
            }
        }

        public bool Equals(Transform2D other)
        {
            return this.origin.Equals(other.origin) &&
                   Math.Abs(this.orientation - other.orientation) < float.Epsilon &&
                   this.scale.Equals(other.scale) &&
                   this.position.Equals(other.position);
        }

        private void UpdateMatrix()
        {
            if (this.originMatrixDirty)
            {
                this.originMatrix = this.origin.HasValue ? Matrix.CreateTranslation(new Vector3(-this.origin.Value, 0f)) : Matrix.Identity;
                this.originMatrixDirty = false;
                this.dirty = true;
            }

            if (this.scaleMatrixDirty)
            {
                this.scaleMatrix = this.scale.HasValue ? Matrix.CreateScale(new Vector3(this.scale.Value, 0f)) : Matrix.Identity;
                this.scaleMatrixDirty = false;
                this.dirty = true;
            }

            if (this.translationMatrixDirty)
            {
                this.translationMatrix = this.position.HasValue ? Matrix.CreateTranslation(new Vector3(this.position.Value, 0f)) : Matrix.Identity;
                this.translationMatrixDirty = false;
                this.dirty = true;
            }

            if (this.rotationMatrixDirty)
            {
                this.rotationMatrix = this.orientation != 0f ? Matrix.CreateRotationZ(this.orientation) : Matrix.Identity;
                this.rotationMatrixDirty = false;
                this.dirty = true;
            }

            if (this.dirty)
            {
                this.transformationMatrix = this.originMatrix *
                                            this.scaleMatrix *
                                            this.rotationMatrix *
                                            this.translationMatrix;
                this.dirty = false;
            }
        }
    }
}
